﻿using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using Framework.DataAccess;
using Project.Domain.Entities;

namespace Project.DAL.Repository
{
    public interface IBetRepository:IRepository<Bet>
    {
        IEnumerable<User> GetUserCreateBet(int matchId);        
    }

    public class BetRepository :RepositoryBase<Bet>, IBetRepository
    {
        private readonly IMatchesRepository _matchesRepository;
        private readonly IUserRepository _userRepository;        

        public BetRepository(IDatabaseFactory databaseFactory,
            IMatchesRepository matchesRepository, 
            IUserRepository userRepository)
            : base(databaseFactory)
        {
            _matchesRepository = matchesRepository;
            _userRepository = userRepository;            
        }

        #region IBetRepository Members

        public IEnumerable<User> GetUserCreateBet(int matchId)
        {
            var userList = (from b in GetAll()
                            join m in _matchesRepository.GetAll() on b.MatchId equals m.MatchId
                            join u in _userRepository.GetAll() on b.UserId equals u.UserId
                            where m.MatchId.Equals(matchId)
                            select u).Distinct();
            return userList;
        }

        #endregion
    }
}